﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using jade.core;
using TravelLib;
using jade.wrapper;
using System.Windows.Forms;

namespace MultiAgentLib
{
    public class MASInit
    {
        private static AgentController _personalAgent;
        private static List<AgentController> _companyAgents;

        public static void InitAgents(Request clientRequest, ref RichTextBox messageTextBox)
        {
            //initialise here the personalAgent and the list of  companies
            jade.wrapper.AgentContainer mainContainer = CreateContainer("MainContainer", true, "localhost",
null, "1090");
            mainContainer.start();
            AgentForm personalAgentForm = new AgentForm();
            personalAgentForm.Text = "Personal Agent";
            _personalAgent = CreateAgent(mainContainer, "PersonalAgent", "MultiAgentLib.PersonalAgent", new object[] { clientRequest, personalAgentForm, messageTextBox });
            _companyAgents = new List<AgentController>();
            int x=0;
            int y=0;
            foreach (Company company in CompanyManager.Companies)
            {
                AgentForm form = new AgentForm();
                form.Text = company.CompanyName;
                form.Location = new System.Drawing.Point(x*form.Width+1, y*form.Height);
                x ++;
                y += (x > 4 ? 1 : 0);
                _companyAgents.Add(CreateAgent(mainContainer, company.CompanyName, "MultiAgentLib.CompanyAgent", new object[] { company, form }));
            }

            foreach (AgentController ag in _companyAgents)
            {
                ag.start();
            }
            _personalAgent.start();
        }

        private static jade.wrapper.AgentContainer CreateContainer(string containerName, bool isMainContainer, string hostAddress, string hostPort, string localPort)
        {
            // Creem un nou profil de configurare pentru container 
            ProfileImpl p = new ProfileImpl();
            // Numele containerului 
            if (containerName != String.Empty)
                p.setParameter(Profile.CONTAINER_NAME, containerName);
            // Containerul este sau nu MainContainer pentru platforma curenta 
            p.setParameter(Profile.MAIN, isMainContainer.ToString());
            // Portul local prin care va comunica cu ceilalti agenti 
            if (localPort != null)
                p.setParameter(Profile.LOCAL_PORT, localPort);
            // Adresa IP a host6ului 
            if (hostAddress != String.Empty)
                p.setParameter(Profile.MAIN_HOST, hostAddress);
            // Portul prin care comunicam cu host6ul 
            if (hostPort != String.Empty)
                p.setParameter(Profile.MAIN_PORT, hostPort);

            if (isMainContainer == true)
            {
                // Cerem crearea containerului principal 
                return Runtime.instance().createMainContainer(p);
            }
            else
            {
                // Cerem crearea containerului 
                return Runtime.instance().createAgentContainer(p);
            }
        }

        private static jade.wrapper.AgentController CreateAgent(jade.wrapper.AgentContainer container, string agentName, string agentClass, object[] args)
        {
            return container.createNewAgent(agentName, agentClass, args);
        }
    }
}
